<html>
<head><meta charset="utf-8"><title>combination div and rem method · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/combination.20div.20and.20rem.20method.html">combination div and rem method</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="224665080"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/combination%20div%20and%20rem%20method/near/224665080" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/combination.20div.20and.20rem.20method.html#224665080">(Jan 31 2021 at 21:15)</a>:</h4>
<p>I could have sworn there was a method in the stdlib on integers to get both the quotient and remainder of a division simultaneously, but I guess I was wrong. is it worth writing up an RFC for this? the benefit (aside from convenience) would be the ability to guarantee that only one division instruction is emitted, but maybe that's trivial enough that LLVM can always do that anyway?</p>



<a name="224665400"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/combination%20div%20and%20rem%20method/near/224665400" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/combination.20div.20and.20rem.20method.html#224665400">(Jan 31 2021 at 21:23)</a>:</h4>
<p>llvm has no trouble combining the operations</p>



<a name="224666357"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/combination%20div%20and%20rem%20method/near/224666357" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/combination.20div.20and.20rem.20method.html#224666357">(Jan 31 2021 at 21:44)</a>:</h4>
<p>IIRC there isn't even a llvm instruction that combines the two so the only viable impl of that method would be to codegen it into separate insns anyway</p>



<a name="224670555"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/combination%20div%20and%20rem%20method/near/224670555" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dirkjan Ochtman <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/combination.20div.20and.20rem.20method.html#224670555">(Jan 31 2021 at 22:50)</a>:</h4>
<p>I've previously wanted this operation for convenience/conciseness reasons</p>



<a name="224670872"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/combination%20div%20and%20rem%20method/near/224670872" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/combination.20div.20and.20rem.20method.html#224670872">(Jan 31 2021 at 22:59)</a>:</h4>
<p>You might be thinking of <code>num::Integer::div_rem</code>: <a href="https://docs.rs/num/0.3.1/num/trait.Integer.html#tymethod.div_rem">https://docs.rs/num/0.3.1/num/trait.Integer.html#tymethod.div_rem</a></p>
<p>(And yes, there's no combined intrinsic in LLVM.)</p>



<a name="225007537"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/combination%20div%20and%20rem%20method/near/225007537" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Poliorcetics <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/combination.20div.20and.20rem.20method.html#225007537">(Feb 03 2021 at 12:23)</a>:</h4>
<p>We have prior art of such combinations with f{32,64}::sin_cos</p>



<a name="225057924"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/combination%20div%20and%20rem%20method/near/225057924" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/combination.20div.20and.20rem.20method.html#225057924">(Feb 03 2021 at 18:22)</a>:</h4>
<p>I have distinct memory of stumbling upon this method or function <em>somewhere</em>, looking its the source in curiosity and finding literally <code>(a / b, a % b)</code></p>



<a name="225058800"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/combination%20div%20and%20rem%20method/near/225058800" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/combination.20div.20and.20rem.20method.html#225058800">(Feb 03 2021 at 18:27)</a>:</h4>
<p>That's the implementation in <code>num</code>, certainly: <a href="https://docs.rs/num-integer/0.1.44/src/num_integer/lib.rs.html#563-565">https://docs.rs/num-integer/0.1.44/src/num_integer/lib.rs.html#563-565</a></p>



<a name="225084431"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/combination%20div%20and%20rem%20method/near/225084431" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/combination.20div.20and.20rem.20method.html#225084431">(Feb 03 2021 at 21:23)</a>:</h4>
<p>LLVM easily turns that into a single <code>div</code> though</p>



<a name="225134187"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/combination%20div%20and%20rem%20method/near/225134187" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kennytm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/combination.20div.20and.20rem.20method.html#225134187">(Feb 04 2021 at 09:06)</a>:</h4>
<p>afaik for 128-bit integers, <code>(a/b, a%b)</code> still won't generate a <code>udivmodti4</code> <a href="https://github.com/rust-lang/rust/issues/44545">https://github.com/rust-lang/rust/issues/44545</a></p>



<a name="225146528"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/combination%20div%20and%20rem%20method/near/225146528" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/combination.20div.20and.20rem.20method.html#225146528">(Feb 04 2021 at 11:21)</a>:</h4>
<p>IIRC it's more an artifact of llvm not knowing about this intrinsic. If it did it could use it. But adding this knowledge would involve changing various built-ins libraries too</p>



<a name="225198105"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/combination%20div%20and%20rem%20method/near/225198105" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/combination.20div.20and.20rem.20method.html#225198105">(Feb 04 2021 at 18:03)</a>:</h4>
<p>FWIW, I do really like <code>div_rem</code> as a method.  It's certainly wasn't obvious to me back in the day that doing them separately was "right" until I learned more about LLVM.  And getting the tuple is quite convenient.</p>
<p>As a bonus, it leads nicely into <code>div_mod</code>, giving a nice opportunity to talk about the difference between <code>rem</code> and <code>mod</code> with signed numbers, and where doing them together is potentially more justifiable since both the resulting values need a fixup.  (CSE might get rid of the duplication anyway, but hard to say.)</p>
<p>Could even do <code>div_mod</code> where the modulus is always an unsigned type...</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>